import util from "../../util";
export default class eventModel {
    constructor() {
        this.util=util;
        this.filter=null;
        this.record={};
        this.pointer=null;
    }
    setRecord(record){
        if(!!record.category&&!!record.name){
            this.record=record;
            this._setFilter();
        }else{
            this.record={};
            this._setFilter(false);
        }

    }
    static create(record){
        let object=new eventModel();
        if(!!record){
            object.setRecord(record);
        };
        return object;
    }
    _getFilter(array){
        let filter='';
        if(util.tools.testType(array)=='array'){
            filter= `category='${array[0]}'&&name='${array[1]}'`
        }else if(util.tools.testType(array)=='object'){
            filter= `category='${array.category}'&&name='${array.name}'`
        }else if(util.tools.testType(array)=='string'&&array.split(',').length==2){
            let arr=array.split(',');
            filter= `category='${arr.category}'&&name='${arr.name}'`
        }else{
            filter= array;
        }
        filter=`${filter}`
        return filter;
    }
    _setFilter(empty=true){
        if(!empty){
            this.filter=null;
            this.pointer=null;
            return;
        }
        this.filter=[this.record.category,this.record.name];
        this.pointer={
            category:this.record.category,
            name:this.record.name,
            id:this.record.id,
            version:this.record.version,
            published:this.record.published,
        };
    }
    async add(record){
        if(!record||!record.category||!record.name||record.category==''||record.name==''){
            return false;
        }
        let newRecord=Object.assign(eventModel.tpl(),record);
        let result = await util.api.Event.add(newRecord).then(data=>{
            if(!!data){
                this.setRecord(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async delete(){
        if(!!this.filter){
            let result = await util.api.Event.delete(this.pointer.id).then(flag=>{
                if(flag){
                    this.setRecord([]);
                    return true;
                }else{
                    return false;
                }
            }).catch(err=>{
                return false;
            });
            return result;
        }else{
            return false;
        }


    }
    async update(){
        let result = await util.api.Event.update(this.record).then(flag=>{
            if(flag){
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async query(category,name){
        let filter=`category='${category}'&&name='${name}'`;
        let result = await util.api.Event.queryIncludeUnpublished({condition:filter}).then(data=>{
            if(!!data){
                this.setRecord(data[0]);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    addServiceItem(index){
        this.record.event.service.splice(index+1,0,eventModel.serviceItemTpl())
    }
    removeServiceItem(index){
        this.record.event.service.splice(index,1);
    }

    static tpl(){
        return {
            category: "",
            name: "",
            version: 1,
            published: true,
            event:eventModel.eventTpl(),
            // createTime: dayjs.localeData(),
            // updateTime: (new Date()).toLocaleDateString(),
            comments: ""
        }
    }
    static eventTpl(){
        return {
            state:{},
            param:{},
            service:[],
            back:{
                type:'',
                data:'',
                flag:false,
            }
        }
    }
    static serviceItemTpl(){
        return {
            model:'',
            action:'',
            param:{},
            where:{
                type:'',
                className:'',
                id:'',
                condition:''
            },
            saveTo:''
        }
    }
}